package com.shudes.game;

import java.util.*;

import com.shudes.pt.pojo.*;

abstract public class CardCollectionFactory {

	public static Card card(String s) { 
		String suite;
		String rank;
		
		if(s==null || s.length()!=2)
			return null; 
		
		rank = s.substring(0,1); 
		suite = s.substring(1,2); 
		
		return new Card(Rank.byName(rank), Suit.byName(suite)); 
	}
	
	public static HoleCards holeCards(String c1, String c2) { 
		return new HoleCards(card(c1), card(c2)); 
	}
	
	public static HoleCards holeCards(String s) { 
		ArrayList<Card> cards; 
		
		cards = parse(s); 
		return (cards.size()==2) ? new HoleCards(cards.get(0), cards.get(1)) : null;  
	}
	
	public static HoleCards holeCards(GamePlayer gp) { 
		return (gp.getHoleCard1()==null) ? null : 
			holeCards(gp.getHoleCard1(), gp.getHoleCard2());  
	}
	
	public static Board board(String b) { 
		return new Board(parse(b));  
	}
	
	public static CardCollection cardCollection(String s) { 
		return new CardCollection(parse(s)); 
	}
	
	protected static ArrayList<Card> parse(String b) { 
		StringTokenizer t;
		String s;
		ArrayList<Card> cards;
		
		cards = new ArrayList<Card>(); 
		
		t = new StringTokenizer(b, " ",false); 
		while(t.hasMoreTokens()) {
			s = t.nextToken();
			if(s!=null && s.length()==2) 
				cards.add(card(s));				
		}
		
		return cards; 
	}
	
	public static Board board(Game g) { 
		Collection<Card> cards; 
		
		cards = new ArrayList<Card>();
		
		if(g.getFlop1()!=null) 
			cards.add(card(g.getFlop1()));
		
		if(g.getFlop2()!=null) 
			cards.add(card(g.getFlop2())); 
		
		if(g.getFlop3()!=null) 
			cards.add(card(g.getFlop3())); 
		
		if(g.getTurn()!=null) 
			cards.add(card(g.getTurn())); 
		
		if(g.getRiver()!=null) 
			cards.add(card(g.getRiver())); 
		
		return new Board(cards); 
	}		
}
